package br.com.dev.tcc.view;

import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import br.com.dev.tcc.R;
import br.com.dev.tcc.bean.BaseItem;
import br.com.dev.tcc.model.LocaisModel;
import br.com.dev.tcc.singleton.LocationSingleton;
import br.com.dev.tcc.util.ConnectivityUtil;
import br.com.dev.tcc.util.GpsUtil;
import br.com.dev.tcc.view.dialog.SettingsDialog;

public class LocaisActivity extends DefaultSupportActionBarActivity {

	private LocaisView view;
	private LocaisModel model;

	private SettingsDialog connDialog;
	private SettingsDialog gpsDialog;

	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.grid_base);
		setActionBar(R.string.locais, R.string.locais_subtitulo,
				R.drawable.locais);
		setModel();
		setView();
	}

	@Override
	protected void onResume() {

		super.onResume();
		verifyConnection();
	}
	
	@Override
	protected void onStop() {

		super.onStop();
		if(gpsDialog != null && gpsDialog.isShowing())
			gpsDialog.dismiss();
	}

	private void setModel() {

		model = new LocaisModel(this);
	}

	private void setView() {

		view = new LocaisView(this);

		view.setGridViewItens(model.getGridItens());

		view.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> a, View view, int position,
					long id) {

				onMapItemClick(model.getGridItens().get(position));
			}
		});
	}

	private void verifyConnection() {

		ConnectivityUtil conUtil = new ConnectivityUtil(this);

		if (conUtil.isConnected() || conUtil.isConnecting())
			removeConnDialog();
		else
			showDialogConnectionSettings();
	}

	private void showDialogConnectionSettings() {

		connDialog = new SettingsDialog(this);

		connDialog.setPositiveButtonListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				startActivity(ConnectivityUtil.getConnectionSettingsIntent());
			}
		});

		connDialog.setNegativeButtonListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				finish();
			}
		});

		connDialog.setOnCancelListener(new OnCancelListener() {

			@Override
			public void onCancel(DialogInterface dialog) {

				finish();
			}
		});

		connDialog.show();
	}

	private void removeConnDialog() {

		if (connDialog != null && connDialog.isShowing()) {

			connDialog.dismiss();
			connDialog = null;
		}
	}
	
	private void showDialogGpsSettings() {

		gpsDialog = new SettingsDialog(this);
		gpsDialog.setCancelable(false);
		gpsDialog.setMessage(R.string.sem_gps_dialog);
		
		gpsDialog.setPositiveButtonListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				startActivity(GpsUtil.getGpsSettingsIntent());
			}
		});

		gpsDialog.setNegativeButtonListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				finish();
			}
		});

		gpsDialog.show();
	}
	
	private void onMapItemClick(BaseItem item){

		if(isGpsEnabled()){
			if(hasLocation())
				startActivity(item.getIntent());
			else{
				Toast.makeText(this, R.string.obtendo_local, Toast.LENGTH_LONG).show();
			}
		}
		else
			showDialogGpsSettings();
	}
	
	private boolean isGpsEnabled(){
		GpsUtil util = new GpsUtil(this);
		return util.isGpsEnabled() || util.isGpsNetEnabled();
	}
	
	private boolean hasLocation(){
		return LocationSingleton.getInstance() != null 
				&& LocationSingleton.getInstance().getLocation() != null;
	}
}
